//
//  UIImage+Extern.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/30.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

extension UIImage {
    
    
    
    /// 获取当前屏幕的截屏图片
    class func zm_getScreenShot() -> UIImage{
        
        //获取widow
        let window = UIApplication.shared.keyWindow!
        //开启图像上下文 opaque:图像透明开关
        UIGraphicsBeginImageContextWithOptions(window.size, true, 0)
        //截屏区域
        let  rect  = CGRect(origin: CGPoint.zero, size: window.size)
        //将window渲染在截屏区域
        window.drawHierarchy(in: rect, afterScreenUpdates: true)
        //获取渲染后图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //结束图像上下文
        UIGraphicsEndImageContext()
        
        return image!
        
    }
    
    //压缩图片
    func scaleImage(scaleWidth:CGFloat)->(UIImage){
        //计算压缩后的高度
        let  scaleHeight = scaleWidth/self.size.width * self.size.height
        //压缩后的大小
        let scaleSize = CGSize(width: scaleWidth, height: scaleHeight)
        //开启图像上下文
        UIGraphicsBeginImageContext(scaleSize)
        let rect = CGRect(origin: CGPoint.zero, size: scaleSize)
        
        //将图片渲染在指定的区域
        self.draw(in: rect)
        //从渲染区域中获取图片
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else{
            return self
        }
        //关闭图形上下文
        UIGraphicsEndImageContext()
        return image
        
    }
    
    
}


